<?php // class DB - ver. 0.1 
// 
// Autore: Eugenio Ghiglia 
//

class Db {

    public  $username;     // Utente utilizzato per la connessione
    public  $password;     // password utilizzata per la connessione
    public  $database;     // database sul quale connettersi
    public  $host;         // ip del database server
    public  $view_error;   // visualizza errori del DB
    public  $connessione;  // connessione al DB
    public  $result;       // risultati di una query
    public  $nrecord;      // totale record dell'ultima query eseguita
    public  $resultset;    // array con i record di una select
    public  $lastid;       // ultimo id utilizzato
    public  $newconn;      // stabilisce se deve essere aperta una nuova connessione
    public  $log_path;     // percorso log errori
    public  $log_enable;   // abilitazione log
    public  $last_msg;     // ultimo messaggio (mysql o della classe)


    function Db($log_path='', $log_enable=true) {  /* Costruttore */
       $this->username = "root";
       $this->password = "1234";
       $this->database = "mysql";
       $this->host = "127.0.0.1";
       $this->view_error=false;
       $this->nrecord=0;
       $this->lastid=0;
       $this->newconn=0;
       $this->log_path=$log_path;
       $this->log_enable=$log_enable;
       if (!$this->log_path) $this->log_path='./class_db_error_log';
       if ($this->log_enable) {
            if (!@is_dir($this->log_path)) {
                mkdir($this->log_path);
            }
       }
    }


    function connetti($host, $user, $password, $db) { /* apre una connessione al db */
       $returned = true;
       $this->username = $user;
       $this->password = $password;
       $this->database = $db;
       $this->host = $host;
       
       $this->connessione = @mysql_connect($this->host, $this->username, $this->password, $this->newconn);
            
        if (!$this->connessione) {
            $this->last_msg='Error ['.@mysql_errno().']: unable to connect to '.$this->host.' '.mysql_error();
            if ($this->view_error) echo $this->last_msg;
            if ($this->log_enable) {
                $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n");
                fclose($fp);
            }
            $returned = false;
        } else {
            if (!@mysql_select_db($this->database, $this->connessione)) {
                $this->last_msg='Error ['.@mysql_errno($this->connessione).']: '.@mysql_error($this->connessione);
                if ($this->view_error) echo $this->last_msg;
                if ($this->log_enable) {
                    $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                    fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n");
                    fclose($fp);
                }
                $returned = false;
            }
        }
        
       return $returned;
    }
    
    function disconnetti() { /* chiude la connessione al db */
        $returned = true;
        if (!@mysql_close($this->connessione) && $this->view_error) {
            $this->last_msg='Error ['.@mysql_errno($this->connessione).']: '.@mysql_error($this->connessione);
            if ($this->view_error) echo $this->last_msg;
            if ($this->log_enable) {
                $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n");
                fclose($fp);
            }
            $returned = false;
        }
        return $returned;
    }

    function error_off() { /* disabilita la visualizzazione dei messaggi */
        $this->view_error=false;
    }

    function error_on() { /* abilita la visualizzazione dei messaggi */
        $this->view_error=true;
    }
    
     function change_db($new_db) {
       $returned = false;
       $old = $this->database;
       $this->database = $new_db;
       
       /* selezione del database da usare */
       $selezionedb=@mysql_select_db($this->database, $this->connessione);
       
       if (!$selezionedb && $this->view_error) {
            $this->last_msg='Error ['.@mysql_errno($this->connessione).']: '.@mysql_error($this->connessione);
            if ($this->view_error) echo $this->last_msg;
            if ($this->log_enable) {
                $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n");
                fclose($fp);
            }
            $returned = false;
            $this->database = $old;
        }
        return $returned;
    } // fine metodo change_db



    function sql($sql, &$result='', &$nrecord='') { /* esegue uno statement SQL */
        $returned = true;
        if ($sql!="") {
            $this->lastid=0;
            if (!is_array($result)) {
                $this->result=@mysql_query($sql, $this->connessione);
                if (!$this->result) {
                    $this->last_msg='Error ['.@mysql_errno($this->connessione).']: '.@mysql_error($this->connessione);
                    if ($this->view_error) echo $this->last_msg;
                    if ($this->log_enable) {
                        $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                        fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n---\n".$sql."\n---\n");
                        fclose($fp);
                    }
                    $returned = false;
                } else {
                  $this->nrecord=@mysql_num_rows($this->result);
                  $this->lastid=@mysql_insert_id($this->connessione);
                  $returned = true;
                }
            } else {
                $result=Array();
                $nrecord=0;
                $this->result=@mysql_query($sql, $this->connessione);
                if (!$this->result) {
                    $this->last_msg='Error ['.@mysql_errno($this->connessione).']: '.@mysql_error($this->connessione);
                    if ($this->view_error) echo $this->last_msg;
                    if ($this->log_enable) {
                        $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                        fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n---\n".$sql."\n---\n");
                        fclose($fp);
                    }
                    $returned = false;
                } else {
                  $this->nrecord=@mysql_num_rows($this->result);
                  $nrecord=$this->nrecord;
                  $i=0;
                  // memorizzo i dati nell'array
                  while ($row=@mysql_fetch_array($this->result)) {
                    $ncampi = mysql_num_fields($this->result);
                    for ($j=0; $j<$ncampi; $j++) {
                      $campo = mysql_field_name($this->result, $j);
                      $result[$i][$campo]=$row[$campo];
                    }
                    $i++;
                  }     
                  $returned = true;
                }
            }
        } else {
            $this->last_msg='Empty SQL';
            if ($this->view_error) echo $this->last_msg;
            if ($this->log_enable) {
                $fp=fopen($this->log_path.'/'.date('Ymd').'.log','a');
                fwrite($fp, date('H:i:s.u').': '.$this->last_msg."\n");
                fclose($fp);
            }
            $returned = false;
        }
        return $returned;
    }

}

?>